package com.sari.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by WANGSF on 2015/7/9.
 */
public class HttpClient {

    private static Logger logger = LoggerFactory.getLogger(HttpClient.class);
    public static JSONObject get(String url) {

        System.out.println(url);
        CloseableHttpClient httpclient = HttpClients.createDefault();

        CloseableHttpResponse response = null;
        try {

            HttpGet httpget = new HttpGet(url);

            response = httpclient.execute(httpget);

            HttpEntity entity = response.getEntity();


            if (entity != null) {
                return JSONObject.parseObject(EntityUtils.toString(entity,
                        "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
                httpclient.close();

            } catch (Exception e2) {
                e2.printStackTrace();
            }

        }
        return null;
    }

    public static void main(String[] args){
        String url = "http://120.76.230.110:8088/StandardApiAction_login.action";
        JSONObject temp = new JSONObject();
        temp.put("account","OKSH001");
        temp.put("password","000000");
        JSONObject json = post(url,temp);
        logger.info("json:"+json.toJSONString());
    }

    public static JSONObject post(String url, JSONObject params) {

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        Set<String> keys = params.keySet();
        for (String key : keys) {
            if (params.get(key) != null) {
                formparams.add(new BasicNameValuePair(key, params.get(key)
                        .toString()));
            }
        }

        UrlEncodedFormEntity uefEntity;
        try {

            HttpPost httpPost = new HttpPost(url);
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");

            httpPost.setEntity(uefEntity);
            response = httpclient.execute(httpPost);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return JSONObject.parseObject(EntityUtils.toString(entity,
                        "UTF-8"));
            }
        } catch (Exception e) {

        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static JSONObject put(String url, JSONObject params) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;

        List<NameValuePair> formparams = new ArrayList<NameValuePair>();

        Set<String> keys = params.keySet();
        for (String key : keys) {
            if (params.get(key) != null) {
                formparams.add(new BasicNameValuePair(key, params.get(key)
                        .toString()));
            }
        }

        UrlEncodedFormEntity uefEntity;
        try {
            HttpPut httpPut = new HttpPut(url);
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httpPut.setEntity(uefEntity);
            response = httpclient.execute(httpPut);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return JSONObject.parseObject(EntityUtils.toString(entity,
                        "UTF-8"));
            }
        } catch (Exception e) {

        } finally {
            try {
                response.close();
                httpclient.close();

            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static JSONObject delete(String url) {

        System.out.println(url);

        CloseableHttpClient httpclient = HttpClients.createDefault();
        CloseableHttpResponse response = null;

        try {
            HttpDelete httpDelete = new HttpDelete(url);

            response = httpclient.execute(httpDelete);

            HttpEntity entity = response.getEntity();

            if (entity != null) {
                return JSONObject.parseObject(EntityUtils.toString(entity,
                        "UTF-8"));
            }
        } catch (Exception e) {

        } finally {
            try {
                response.close();
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
}
